<?php

namespace Home\Controller;

/**
 * 订单展示页面
 * @modify trainyao 2016-01-07 创建
 */
class OrderPageController extends ParentController {

    protected function _initialize() {
        parent::_initialize();

        //检查是否已到期
        $hotelLogic = A('Common/Hotel', 'Logic');
        $result = $hotelLogic->checkHotelActive($this->hotelId);
        if ($result == false) {
            $this->messageTips(L('_HOTEL_EXPIRED_'));
        }
        //检查操作权限
        $adminAuthorityLogic = A('Common/AdminAuthority', 'Logic');
        $adminAuthorityLogic->check($this->hotelId);
    }

    /**
     * 订单中心页面
     * @modify trainyao 2016-06-28 增加房间权限判断
     */
    public function index() {
        $param = I('get.');

        $startDate = $param['start'];
        $startDate = empty($startDate) ? date('Y-m-01') : $startDate;
        $endDate = $param['end'];
        $endDate = empty($endDate) ? date('Y-m-d') : $endDate;
        $dateStatus = $param['datestatus'];

        $startTime = strtotime($startDate);
        $endTime = strtotime($endDate);
        // 最多只能查询30天
        if (getTimeDiff($startTime, $endTime, 'day') >= 30) {
            $endDate = date('Y-m-d', strtotime('+30 days', $startTime));
        }


        // 如果时间状态传入格式不对,使其筛选条件失效
        if (!in_array($dateStatus, array('order', 'checkin', 'checkout'))) {
            $dateStatus = 'order';
        }

        $hotelId = $param['hotel'];
        $name = empty($param['name']) ? null : $param['name'];
        $room = empty($param['room']) ? null : $param['room'];
        $orderStatus = is_null($param['status']) ? null : $param['status'];
        $hotelChannelId = empty($param['channel']) ? null : $param['channel'];
        $pageIndex = empty($param['p']) ? 1 : intval($param['p']);
        $pageIndex = $pageIndex > 0 ? $pageIndex : 1;
        $pageSize = C('PAGE_SIZE');

        $orderLogic = A('Common/Order', 'Logic');
        try {
            $orderInfo = $orderLogic->getSubOrder($hotelId, null, $dateStatus, $startDate, $endDate, $room, $orderStatus, $hotelChannelId, $name, $pageIndex, $pageSize, true);
        } catch (\Exception $e) {
            returnJson($e->getMessage());
            return;
        }
        //设置link
        $link = "order_###." . C('URL_HTML_SUFFIX');
        // 检查链接参数
        if ($hotelId || $dateStatus || $startDate || $endDate || $room || $orderStatus || $hotelChannelId || $name) {
            $link = $hotelId ? $link . "?hotel=" . $hotelId : $link;
            $link = $dateStatus ? $link . "&datestatus=" . $dateStatus : $link;
            $link = $startDate ? $link . "&start=" . $startDate : $link;
            $link = $endDate ? $link . "&end=" . $endDate : $link;
            $link = $room ? $link . "&room=" . $room : $link;
            $link = $orderStatus ? $link . "&status=" . $orderStatus : $link;
            $link = $hotelChannelId ? $link . "&channel=" . $hotelChannelId : $link;
            $link = $name ? $link . "&name=" . $name : $link;
        }

        $pageUtil = classpage($orderInfo['page']['total'], $link);
        $pageUtil->setConfig('size', 'small');
        $pageFooter = $pageUtil->show();

        // 格式化数据
        foreach ($orderInfo['subOrder'] as &$subOrder) {
            $subOrder['startdate'] = date('m/d', strtotime($subOrder['startdate']));
            $subOrder['enddate'] = date('m/d', strtotime($subOrder['enddate']));
            $subOrder['createtime'] = date('m/d H:i', strtotime($subOrder['createtime']));
            $subOrder['name'] = implode(',', array_column($subOrder['user'], 'name'));
            foreach ($subOrder['user'] as $key => $user) {
                if (!empty($user['mobile'])) {
                    $subOrder['mobile'] = '+' . $user['areaCode'] . ' '. $user['mobile'] . ' ';
                }
            }
            /*$subOrder['mobile'] = array_diff(array_column($subOrder['user'], 'mobile'), array(''));
            $subOrder['mobile'] = implode(',', $subOrder['mobile']);*/
            $subOrder['identity'] = array_diff(array_column($subOrder['user'], 'identity'), array(''));
            $subOrder['identity'] = implode(',',  $subOrder['identity']);
        }
        unset($subOrder);

        // 房型和房间号
        $roomTypeLogic = A('Common/RoomType', 'Logic');
        $roomInfo = $roomTypeLogic->getInfoByHotel($hotelId, true);
        // 判断是否需要根据房间权限
        $adminId = getUserId();
        $adminAuthorityLogic = A('Common/AdminAuthority', 'Logic');
        if (!$adminAuthorityLogic->checkIsAdmin($adminId, $hotelId)) {
            $accountLogic = A('Common/Account', 'Logic');
            $roomAuthority = $accountLogic->getAdminRoomAuthority($adminId, $hotelId);
            foreach ($roomInfo as $index => &$roomTypeItem) {
                foreach ($roomTypeItem['room'] as $roomIndex => $roomItem) {
                    if (!in_array($roomItem['id'], $roomAuthority)) {
                        unset($roomTypeItem['room'][$roomIndex]);
                    }
                }
                unset($roomItem);

                if (empty($roomTypeItem['room'])) {
                    unset($roomInfo[$index]);
                } else {
                    $roomTypeItem['room'] = array_values($roomTypeItem['room']);
                }
            }
            unset($roomTypeItem);
        }

        $roomsCount = 0;
        if (!empty($roomInfo)) {
            foreach ($roomInfo as $roomType) {
                if (!empty($roomType['room'])) {
                    $roomsCount += count($roomType['room']);
                }
            }
        }
        $this->assign('roomsCount', $roomsCount);
        $this->assign('rooms', $roomInfo);

        // 支付方式
        $hotelLogic = A('Common/Hotel', 'Logic');
        $hotelPayData = $hotelLogic->getHotelPay($hotelId, false, null, true);
        $this->assign('hotelpay', $hotelPayData);
        $noCardhotelPayData = $hotelLogic->getHotelPay($hotelId, false);
        $this->assign('noCardhotelPayData', $noCardhotelPayData);
        // 其他消费项目数据
        $otherCostData = $hotelLogic->getHotelOtherCost($hotelId);
        $this->assign('otherCost', $otherCostData);

        // 获取系统货币
        $currencyLogic = A('Common/Currency', 'Logic');
        $currencyData = $currencyLogic->getBpCurrency($hotelId);
        $currencyDefaultData = my_sort($currencyData, 'type', SORT_DESC)[0];
        $this->assign('currencyInfo', $currencyData);
        $this->assign('currencyDefaultInfo', $currencyDefaultData);

        // 渠道
        $hotelChannelData = $hotelLogic->getHotelChannel($hotelId, false);
        $this->assign('hotelchannel', $hotelChannelData);
        $this->assign('dateStatus', $dateStatus);
        $this->assign('startDate', $startDate);
        $this->assign('endDate', $endDate);
        $this->assign('filterRoomId', $room);
        $this->assign('filterChannelId', $hotelChannelId);
        $this->assign('filterOrderStatus', $orderStatus);
        $this->assign('filterName', $name);
        $this->assign('order', $orderInfo['subOrder']);
        $this->assign('orderStatus', $orderLogic->statusMap[LANG_SET]);
        $this->assign('pageFooter', $pageFooter);
        $this->assign('pageInfo', $orderInfo['page']);


        $this->display();
    }

}
